""" System imports """
import os, sys
import logging

""" Other modules """
import pygame

""" Game """
from constants import *
import inputhandler
from eventrouter import eventpanel
import ui.button
from inventory import Inventory
from items import *
#import resourcemanager

""" Set up logging """
_log = logging.getLogger("epicquest.player")
_log.debug("Initializing Player")

""" Class to represent the player """
class Player():
    
    def __init__(self, *args, **kwargs):
        """ Configurable options """
        self.coords = kwargs.get('coords', (1, 0))
        
        _log.debug("Initializing player with coords " + str(self.coords))
        
        self.inventory = kwargs.get('inventory', Inventory())
        self.view_dist = kwargs.get('view_dist', 5)
                
        """ 'Private' variables """
        self.view_rect = pygame.rect.Rect((0, 0), SCREEN_SIZE)
        self.view_rect.center = (self.coords[0] * TILE_WIDTH, self.coords[1] * TILE_HEIGHT)
        
        self.inventory.addItem(Money(count=10))
                
        
    def draw(self, surface):
        playerfont = pygame.font.SysFont("Courier New", 22)
        player = playerfont.render("@", True, (255, 255, 255))
        surface.blit(player, (self.coords[0] * TILE_WIDTH, self.coords[1] * TILE_HEIGHT))
        
    def move(self, new_coords):
        if self.coords != new_coords:
            self.coords = new_coords
            self.view_rect.center = (self.coords[0] * TILE_WIDTH, self.coords[1] * TILE_HEIGHT)
            
            """ Fire the PLAYER_MOVE event """
            eventpanel.fire_event(self, eventpanel.PLAYER_MOVE, player=self, coords=self.coords)            
        
    def update(self, session):
        pass
    
    """ Expects object to have a coords property
    """
    def isWithin(self, object, distance):
        xdif = self.coords[0] - object.coords[0]
        ydif = self.coords[1] - object.coords[1]
        
        if (xdif <= distance and xdif >= -distance) and (ydif <= distance and ydif >= -distance):
            return True
        return False
        
            
        
